// pages/ucenter/payList/index.js

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: "",  //账户余额
    currentData: 0,
    recharges: [],  //充值
    consumptions: [], //消费
    supplements: [] //补缴
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getUserData(){
    let that=this;
    wx.request({
      url: app.globalData.basicUrl+'user/userData/'+wx.getStorageSync('userId'),  //余额
      success: function (res) {
        console.log(res);
        that.setData({
          money: (res.data.data.money/100).toFixed(2)
        })
      }
    })
  },
  getExpenseList(url){
    let that=this,data={};
    if(url.search('userId')==-1){
      data = { 
        userId: wx.getStorageSync('userId'),
        pageNum:1,
        pageSize:100
      }
    }else{
      data={}
    }
    wx.request({
      url: app.globalData.basicUrl+url,//消费
      data:data,
      success: function (res) {
        console.log(res);
        if(res.statusCode==200){
          if (url == 'user/expense/userId/' + wx.getStorageSync('userId')){
            let recharges=res.data.data.reverse()
            recharges.map(item=>{
              item.amount=item.amount/100
            })
            that.setData({
              recharges: recharges
            })
          } else if (url == 'user/userOrder/orderPay/' + wx.getStorageSync('userId')){
            that.setData({
              consumptions: res.data.reverse()
            })
          }else{
            let supplements=res.data.rows.reverse()
            for (let i = 0; i < supplements.length;i++){
              supplements[i].paymentTime = supplements[i].paymentTime.split(' ')[0]
            }
            that.setData({
              supplements: supplements
            })
          }
        }
      }
    })
  },
  //获取当前滑块的index
  bindchange: function (e) {
    this.setData({
      currentData: e.detail.current
    })
    if(e.detail.current==0){
      this.getExpenseList('user/expense/userId/' + wx.getStorageSync('userId'))
    } else if (e.detail.current == 1){
      this.getExpenseList('user/userOrder/orderPay/' + wx.getStorageSync('userId'))
    }else{
      this.getExpenseList('user/payment/list')
    }
    this.options.tab = e.detail.current
  },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    const that = this;

    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {

      that.setData({
        currentData: e.target.dataset.current
      })
    }
  },
  supplement(e){
    console.log(e,'补缴列表参数')
    wx.navigateTo({
      url: '/pages/supplement/index?oid=' + e.currentTarget.dataset.oid
    })
  },
  stopPayDetail: function (e) {
    console.log("缴费", e);
    let order = e.currentTarget.dataset.item
    if(order){
      wx.navigateTo({
        url: '/pages/stopPayDetail/index?item=' + order
      })
    }else{
      wx.navigateTo({
        url: '/pages/stopPayDetail/index?exid=' + e.currentTarget.dataset.exid
      })
    }
  },
  payDetail: function () {
    wx.navigateTo({
      url: '/pages/payDetail/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      currentData: this.options.tab
    })
    this.getUserData()
    this.getExpenseList('user/expense/userId/' + wx.getStorageSync('userId'))
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

//http://stcar.easy.echosite.cn/expense/userId/{userId}  //充值
//http://stcar.easy.echosite.cn/userOrder/userId/old/{userId} get提交  消费